from django.db import models

from content_center.const import ContentType
from user_center.models import User


class UniversityLabel(models.Model):
    university_name = models.CharField(max_length=128, verbose_name="标签名称")
    competent_department = models.CharField(max_length=128, verbose_name="主管部门")
    location = models.CharField(max_length=128, verbose_name="所在地")
    running_level = models.CharField(max_length=128, verbose_name="办学层次")
    is_deleted = models.BooleanField(default=False, verbose_name="已删除")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "大学标签"
        verbose_name_plural = "大学标签"

    def __str__(self):
        return self.university_name


class SportLabel(models.Model):
    sport_name = models.CharField(max_length=128, verbose_name="标签名称")
    is_deleted = models.BooleanField(default=False, verbose_name="已删除")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "运动标签"
        verbose_name_plural = "运动标签"

    def __str__(self):
        return self.sport_name


class Content(models.Model):
    content_type = models.SmallIntegerField(
        choices=ContentType.choices, verbose_name="内容类型"
    )
    author = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="作者")
    title = models.CharField(max_length=128, verbose_name="标题")
    brief_introduction = models.CharField(
        max_length=256, null=False, default="", verbose_name="简介"
    )
    content_text = models.TextField(verbose_name="内容正文", null=False, default="")
    cover = models.CharField(max_length=256, default="", verbose_name="封面")
    university_labels = models.ManyToManyField(
        UniversityLabel, null=True, verbose_name="高校标签"
    )
    sport_labels = models.ManyToManyField(SportLabel, null=True, verbose_name="运动标签")
    is_deleted = models.BooleanField(default=False, verbose_name="已删除")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "内容"
        verbose_name_plural = "内容"

    def __str__(self):
        return self.title


class Comment(models.Model):
    parent = models.ForeignKey(
        "self", on_delete=models.PROTECT, null=True, verbose_name="父评论"
    )
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="作者")
    content = models.ForeignKey(
        Content, on_delete=models.PROTECT, null=True, verbose_name="内容"
    )
    text = models.CharField(max_length=512, verbose_name="评论正文")
    is_deleted = models.BooleanField(default=False, verbose_name="已删除")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "评论"
        verbose_name_plural = "评论"

    def __str__(self):
        return self.text


class ContentLike(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="作者")
    content = models.ForeignKey(Content, on_delete=models.PROTECT, verbose_name="内容")
    is_deleted = models.BooleanField(default=False, verbose_name="已删除")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "内容点赞"
        verbose_name_plural = "内容点赞"

    def __str__(self):
        return self.user.username


class CommentLike(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="作者")
    comment = models.ForeignKey(Comment, on_delete=models.PROTECT, verbose_name="评论")
    is_deleted = models.BooleanField(default=False, verbose_name="已删除")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "评论点赞"
        verbose_name_plural = "评论点赞"

    def __str__(self):
        return self.user.username


class ContentCollect(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="作者")
    content = models.ForeignKey(Content, on_delete=models.PROTECT, verbose_name="内容")
    is_deleted = models.BooleanField(default=False, verbose_name="已删除")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "内容收藏"
        verbose_name_plural = "内容收藏"

    def __str__(self):
        return self.user.username


class CommentCollect(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="作者")
    comment = models.ForeignKey(Comment, on_delete=models.PROTECT, verbose_name="评论")
    is_deleted = models.BooleanField(default=False, verbose_name="已删除")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "评论收藏"
        verbose_name_plural = "评论收藏"

    def __str__(self):
        return self.user.username


class ContentBrowseHistory(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="作者")
    content = models.ForeignKey(
        Content, null=True, on_delete=models.PROTECT, verbose_name="内容"
    )
    is_deleted = models.BooleanField(default=False, verbose_name="已删除")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "浏览历史"
        verbose_name_plural = "浏览历史"

    def __str__(self):
        return "{}：{}".format(self.user.username, self.content.title)
